<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Report_laporan_biaya extends Authenticated_Controller {
        
	function __construct() {
		parent::__construct();
		$this->load->model('laporan_setoran_model','lap');
		$this->load->model('pencabang_model','cabang');
                
	}

	function index() {
		$this->list_result();
	}

	function list_result($tgl_awal='', $tgl_akhir='',$jenis_biaya='') {
		$data['filter_cabang'] = $this->cabang->get_all_cabang();
		$data['pil_coa']=$this->lap->get_coa();
		
		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'm/d/Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'm/d/Y');
		}
		if($jenis_biaya==''){
			$data['jenis_biaya']='SEMUA';
		}else{
			$data['jenis_biaya']= $jenis_biaya;
		}
				
		$data['coa'] = $this->lap->get_coa();
		$data['list_result'] = $this->lap->get_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['setoran'][$i] = $this->lap->get_setoran_user_by_biaya($data['list_result'][$i]['NoSPJ']);
		}
		$data['sum_tujuan'] = $this->lap->get_jumlah_tujuan_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		$data['sum_asal'] = $this->lap->get_jumlah_asal_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		$this->load->view('template/header_view');
		$this->load->view('laporan/laporan_biaya', $data);
		$this->load->view('template/footer_view');
	}

	function cetak_pdf($tgl_awal='', $tgl_akhir='',$jenis_biaya='') {
		$data['filter_cabang'] = $this->cabang->get_all_cabang();
		$data['pil_coa']=$this->lap->get_coa();
		
		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'm/d/Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'm/d/Y');
		}
		if($jenis_biaya==''){
			$data['jenis_biaya']='SEMUA';
		}else{
			$data['jenis_biaya']= $jenis_biaya;
		}
				
		$data['coa'] = $this->lap->get_coa();
		$data['list_result'] = $this->lap->get_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['setoran'][$i] = $this->lap->get_setoran_user_by_biaya($data['list_result'][$i]['NoSPJ']);
		}
		$data['sum_tujuan'] = $this->lap->get_jumlah_tujuan_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		$data['sum_asal'] = $this->lap->get_jumlah_asal_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		
		$html = $this->load->view('laporan/html_cetak_laporan_biaya', $data,TRUE);
		$this->load->library('pdf');
		$this->pdf->to_pdf_landscape($html,'Laporan Biaya.pdf');
	}
	
	function cetak_excel($tgl_awal='', $tgl_akhir='',$jenis_biaya='') {
		$this->load->library('excel');
		
		$data['filter_cabang'] = $this->cabang->get_all_cabang();
		$data['pil_coa']=$this->lap->get_coa();
		
		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'm/d/Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'm/d/Y');
		}
		if($jenis_biaya==''){
			$data['jenis_biaya']='SEMUA';
		}else{
			$data['jenis_biaya']= $jenis_biaya;
		}
				
		$data['coa'] = $this->lap->get_coa();
		$data['list_result'] = $this->lap->get_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['setoran'][$i] = $this->lap->get_setoran_user_by_biaya($data['list_result'][$i]['NoSPJ']);
		}
		$data['sum_tujuan'] = $this->lap->get_jumlah_tujuan_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		$data['sum_asal'] = $this->lap->get_jumlah_asal_laporan_biaya($tgl_awal, $tgl_akhir,$jenis_biaya);
		
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle('title')->setDescription('description');
		$objPHPExcel->setActiveSheetIndex(0);
		//Set Keterangan
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Biaya');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Perode');
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $data['tgl_awal'].'-'.$data['tgl_akhir']);
		$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Jenis Biaya');
		$objPHPExcel->getActiveSheet()->setCellValue('B3', $jenis_biaya);
		//Set header
		$objPHPExcel->getActiveSheet()->setCellValue('A4', 'No');
		$objPHPExcel->getActiveSheet()->setCellValue('B4', 'Tanggal Pemabayaran');
		$objPHPExcel->getActiveSheet()->setCellValue('C4', 'No. SPJ');
		$objPHPExcel->getActiveSheet()->setCellValue('D4', 'No. Polisi');
		$objPHPExcel->getActiveSheet()->setCellValue('E4', 'Sopir');
		$objPHPExcel->getActiveSheet()->setCellValue('F4', 'Jurusan');
		if ($jenis_biaya == 'SEMUA'){
			$objPHPExcel->getActiveSheet()->setCellValue('G4', 'Jumlah');
			$objPHPExcel->getActiveSheet()->setCellValue('H4', 'Cabang');
			$objPHPExcel->getActiveSheet()->setCellValue('I4', 'Kasir');
		}else{
			$objPHPExcel->getActiveSheet()->setCellValue('G4', 'Jenis Biaya');
			$objPHPExcel->getActiveSheet()->setCellValue('H4', 'Jumlah');
			$objPHPExcel->getActiveSheet()->setCellValue('I4', 'Cabang');
			$objPHPExcel->getActiveSheet()->setCellValue('J4', 'Kasir');
		}
		// Save it as an excel 2003 file
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$row = 5;
		$sum['jenis_biaya'] = '';
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, ($i+1));
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $data['setoran'][$i]['tgl_pembayaran']);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $data['list_result'][$i]['NoSPJ']);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $data['setoran'][$i]['NoPol']);
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $data['setoran'][$i]['driver']);
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $data['setoran'][$i]['asal'] . "-" . $data['setoran'][$i]['tujuan']);
			$sum['jenis_biaya'] +=$data['list_result'][$i]['jumlah'];
			if ($jenis_biaya == 'SEMUA'){
				$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $data['list_result'][$i]['jumlah']);
				$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, 'Cabang');
				$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, 'Kasir');
			}else{
				$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $data['setoran'][$i]['nama_coa']);
				$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $data['list_result'][$i]['jumlah']);
				$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $data['list_result'][$i]['cabang']);
				$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, $data['setoran'][$i]['nama']);
			}
			$row++;
		}
		$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, 'Biaya Awal');
		$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, 'Biaya Awal');
		$objWriter->save('public/Report Biaya '.$tgl_awal.'-'.$tgl_akhir.'.xls');
		redirect(PUBLIC_FOLDER.'Report Biaya '.$tgl_awal.'-'.$tgl_akhir.'.xls');
	}
	
}